//
//  WBTabBar.swift
//  0829-Weibo
//
//  Created by 毕仲阳 on 16/8/29.
//  Copyright © 2016年 mycompany. All rights reserved.
//

import UIKit

class WBTabBar: UITabBar {
    //定义一个闭包属性
    var composeButtonClosure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    //当使用其它初始化方法初始时，抛出错误
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI()
    {
        backgroundImage = #imageLiteral(resourceName: "tabbar_background")
        addSubview(composeButton)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //print(subviews)
        //计算composeButton的位置
        composeButton.center = CGPoint(x: self.bounds.size.width*0.5, y: self.bounds.height*0.5)
        //计算每一个条目的宽度
        let itemW = UIScreen.main.bounds.width/5
        
        var index = 0
        for subView in subviews
        {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                let itemX = CGFloat(index) * itemW
                subView.frame.size.width = itemW
                subView.frame.origin.x = itemX
                index += 1
                if index == 2
                {
                    index = index + 1
                }
            }
        }
    }
    //按钮的点击事件
    func composeButtonClick()
    {
        //print("撰写按钮被点击了")
        composeButtonClosure?()
    }
    
    lazy var composeButton:UIButton =
    {
       let button = UIButton()
        //添加按钮点击事件
        button.addTarget(self, action: #selector(composeButtonClick), for: UIControlEvents.touchUpInside)
        button.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: UIControlState.normal)
        button.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        button.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: UIControlState.normal)
        button.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        button.sizeToFit()
        return button
        
    }()
}
